
class JoyStick extends eui.Component {
    private _backgroud: eui.Image = null
    private _btn: eui.Image = null
    private _touchBtn: eui.Image = null
    private _touchX: number = 0
    private _touchY: number = 0
    private _joyAngle: number = 0
    private _joyRadius: number = 0
    private _joyRadiusPercent: number = 0

    private _joyCenterX: number = 0
    private _joyCenterY: number = 0

    private _maxRadius: number = 0
    private _maxAngle: number = 0
    private _minAngle: number = 0
    private _limitAngle: boolean = false

    private _verticalJoy: boolean = false //垂直
    private _horizontalJoy: Boolean = false //水平

    public get JoyAngle() {
        return this._joyAngle
    }
    public get JoyRadius() {
        return this._joyRadius
    }
    public get JoyRadiusPercent() {
        return this._joyRadius/this._maxRadius
    }

    public set VerticalJoy(b: boolean) {
        this._verticalJoy = b
    }
    public set HorizontalJoy(b: boolean) {
        this._horizontalJoy = b
    }
    
    constructor() {
        super()
    }

    public Init(parent: egret.DisplayObjectContainer, x: number, y: number): void {
        this._backgroud = new eui.Image
        this._backgroud.texture = RES.getRes("joystick_bg_png")
        this._joyCenterX = this._backgroud.width / 2
        this._joyCenterY = this._backgroud.height / 2
        this._backgroud.touchEnabled = false
        this.addChildAt(this._backgroud, 0)

        this.anchorOffsetX = this._joyCenterX
        this.anchorOffsetY = this._joyCenterY

        this._btn = new eui.Image
        this._btn.texture = RES.getRes("joystick_btn_png")
        this._btn.anchorOffsetX = this._btn.width / 2
        this._btn.anchorOffsetY = this._btn.height / 2
        this._btn.x = this._joyCenterX
        this._btn.y = this._joyCenterY
        this._btn.touchEnabled = false
        this.addChildAt(this._btn, 5)

        this._touchBtn = new eui.Image
        this._touchBtn.texture = RES.getRes("joystick_btn_png")
        this._touchBtn.anchorOffsetX = this._touchBtn.width / 2
        this._touchBtn.anchorOffsetY = this._touchBtn.height / 2
        this._touchBtn.x = this._joyCenterX
        this._touchBtn.y = this._joyCenterY
        this._touchBtn.touchEnabled = false
        this.addChildAt(this._touchBtn, 6)

        this._maxRadius = (this._backgroud.width) / 2 - this._btn.width / 2

        parent.addChildAt(this, 1000)
        this.x = x
        this.y = y

        this.addEventListener(egret.TouchEvent.TOUCH_BEGIN, this.onTouchBegin, this)
        this.addEventListener(egret.TouchEvent.TOUCH_MOVE, this.onTouchMove, this)
        this.addEventListener(egret.TouchEvent.TOUCH_END, this.onTouchEnd, this)
        this.addEventListener(egret.TouchEvent.TOUCH_CANCEL, this.onTouchEnd, this)
        this.addEventListener(egret.TouchEvent.TOUCH_RELEASE_OUTSIDE, this.onTouchEnd, this)
    }

    private onTouchBegin(evt: egret.TouchEvent): void {
        this._touchX = evt.localX
        this._touchY = evt.localY
        this.moveJoyStick(evt.localX, evt.localY)
    }
    private onTouchMove(evt: egret.TouchEvent): void {
        this.moveJoyStick(evt.localX, evt.localY)
        //console.log("===>(%d, %d)", evt.localX, evt.localY)
    }
    private onTouchEnd(evt: egret.TouchEvent): void {
        this.moveJoyStick(this._joyCenterX, this._joyCenterY)
    }
    private moveJoyStick(x: number, y: number) {
        if (this._touchBtn != null) {
                this._touchBtn.x = x
                this._touchBtn.y = y
            }

        if (this._verticalJoy) {
            this._joyRadius = y - this._joyCenterY
            if (this._joyRadius >= 0) {
                this._joyAngle = 1
            } else {
                this._joyAngle = -1
                this._joyRadius = -this._joyRadius
            }
            if (this._joyRadius > this._maxRadius) {
                this._joyRadius = this._maxRadius
            }
            this._btn.x = this._joyCenterX
            this._btn.y = this._joyAngle * this._joyRadius + (this._joyCenterY)
        } else if (this._horizontalJoy) {

        } else {
            let dx = x - this._joyCenterX
            let dy = y - this._joyCenterY
            this._joyRadius = Math.sqrt(Math.pow(dx, 2) + Math.pow(dy, 2))
            if (this._joyRadius > this._maxRadius) {
                this._joyRadius = this._maxRadius
            }

            //let sin = dx * y - x * dy;
            //let cos = dx * x + dy * y;
            this._joyAngle = Math.atan2(dy, dx);
            //while (this._joyAngle < 0) { this._joyAngle += (2 * Math.PI); }
            //while (this._joyAngle > 2 * Math.PI) { this._joyAngle -= (2 * Math.PI); }
            if (this._limitAngle) {
                if (this._joyAngle >= 0) {
                    if (this._joyAngle > this._maxAngle) { this._joyAngle = this._maxAngle }
                    if (this._joyAngle < this._minAngle) { this._joyAngle = this._minAngle }
                } else {
                    if (-this._joyAngle > this._maxAngle) { this._joyAngle = -this._maxAngle }
                    if (-this._joyAngle < this._minAngle) { this._joyAngle = -this._minAngle }
                }
            }

            this._joyRadiusPercent = this._joyRadius / this._maxRadius

            this._btn.x = Math.cos(this._joyAngle) * this._joyRadius + (this._joyCenterX)
            this._btn.y = Math.sin(this._joyAngle) * this._joyRadius + (this._joyCenterY)

            console.log("Touch (%d, %d) => (r:%f a:%d)", x - dx, y - dy, this._joyRadiusPercent, this._joyAngle * 180 / Math.PI)
        }
    }
}